package com.gyw.myapplication;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.gyw.myapplication.compare.TableAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by yangyi on 2018/12/8.
 */

public  class CarTableAdapter extends TableAdapter<CarTableAdapter.CornerViewHolder,
        CarTableAdapter.TopCellViewHolder,CarTableAdapter.GroupRowViewHolder,
        CarTableAdapter.LeftCellViewHolder,CarTableAdapter.CellViewHolder,CarTableAdapter.SuspensionViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<CarData> datas;


    /**
     * 该HashMap用于记录所有的row,key为row的position
     * value：如果对应的row为grouprow,则+1，
     *        如果是normalrow则存放的是map:key为属于哪个group,value为group中的position;
     */
    private HashMap<Integer,Object> rowDataLocate=new HashMap<>();

    public CarTableAdapter(List<CarData> datas, Context context){

        this.context=context;
        this.inflater=LayoutInflater.from(context);
        this.datas=datas;
        preData(datas);
    }


    private void preData(List<CarData> datas){
        List<CarData.GroupMessage> groupParamsViewModelList=datas.get(0).getGroupParamsViewModelList();
        int position=0;
        int currentGroupLocate=0;
        for(CarData.GroupMessage groupMessage:groupParamsViewModelList){
            rowDataLocate.put(position,currentGroupLocate);
            position+=1;
            int currentGroupParamLocate=0;
            for(CarData.Param param:groupMessage.getParamList()){
                HashMap<Integer,Integer> hashMap=new HashMap<>();
                hashMap.put(currentGroupLocate,currentGroupParamLocate);
                rowDataLocate.put(position,hashMap);
                position+=1;
                currentGroupParamLocate++;
            }
            currentGroupLocate+=1;
        }
    };

    @Override
    public int getCellRows() {
        return rowDataLocate.size();
    }

    @Override
    public int getCellColumns() {
        return datas.size();
    }

    @Override
    public CornerViewHolder onCreateCornerCellViewHolder(ViewGroup parent) {
        CornerViewHolder cornerViewHolder=new CornerViewHolder(inflater.inflate(R.layout.corner_cell_layout,parent,false));
        return cornerViewHolder;
    }

    @Override
    public void onBindCornerCellViewHolder(CornerViewHolder viewHolder) {
        viewHolder.cornerCell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast("corner被点击");
            }
        });

    }


    @Override
    public TopCellViewHolder onCreateTopCellViewHolder(ViewGroup parent) {
        TopCellViewHolder viewHolder=new TopCellViewHolder(inflater.inflate(R.layout.top_cell_layout,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindTopCellViewHolder(TopCellViewHolder viewHolder, final int columnPosition) {
        viewHolder.top_cell.setText(datas.get(columnPosition).getSpecName());
        viewHolder.top_cell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(datas.size()<=2){
                    showToast("至少需要两项比较");
                }else{
                    datas.remove(columnPosition);
                    notifyDataChanged();
                }

            }
        });
    }

    @Override
    public GroupRowViewHolder onCreateGroupRowViewHolder(ViewGroup parent) {
        GroupRowViewHolder viewHolder=new GroupRowViewHolder(inflater.inflate(R.layout.group_row_view,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindGroupRowViewHolder(GroupRowViewHolder viewHolder, int rowPosition) {
        viewHolder.group_row.setText(datas.get(0).getGroupParamsViewModelList().get((Integer) rowDataLocate.get(rowPosition)).getGroupName());
    }

    @Override
    public LeftCellViewHolder onCreateLeftCellViewHolder(ViewGroup parent) {
        LeftCellViewHolder leftCellViewHolder=new LeftCellViewHolder(inflater.inflate(R.layout.left_cell_view,parent,false));
        return leftCellViewHolder;
    }

    @Override
    public void onBindLeftCellViewHolder(LeftCellViewHolder viewHolder, int rowPosition) {
        HashMap<Integer,Integer> hashMap= (HashMap<Integer, Integer>) rowDataLocate.get(rowPosition);
        for (Integer key:hashMap.keySet()){
            CarData.GroupMessage groupMessage=datas.get(0).getGroupParamsViewModelList().get(key);
            CarData.Param param=groupMessage.getParamList().get(hashMap.get(key));
            viewHolder.left_cell.setText(param.getParamName());
        }
    }


    @Override
    public CellViewHolder onCreateCellViewHolder(ViewGroup parent) {
        CellViewHolder cellViewHolder=new CellViewHolder(inflater.inflate(R.layout.cell_view,parent,false));
        return cellViewHolder;
    }

    @Override
    public void onBindCellViewHolder(CellViewHolder viewHolder, int columnPosition, int rowPosition) {
        HashMap<Integer,Integer> hashMap= (HashMap<Integer, Integer>) rowDataLocate.get(rowPosition);
        for (Integer key:hashMap.keySet()){
            List<CarData.GroupMessage> groupMessages=datas.get(columnPosition).getGroupParamsViewModelList();
            if (groupMessages.size()!=0){
                CarData.GroupMessage groupMessage=groupMessages.get(key);
                CarData.Param param=groupMessage.getParamList().get(hashMap.get(key));
                viewHolder.cell.setText(param.getParamValue());
            }else{
                viewHolder.cell.setText("");
            }

        }

    }



    @Override
    public boolean isGroupView(int position) {
        if(rowDataLocate.get(position) instanceof Integer){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public SuspensionViewHolder onCreateSuspensionViewHolder() {
        //注意吸附布局需要和Group布局一致
        SuspensionViewHolder viewHolder=new SuspensionViewHolder(inflater.inflate(R.layout.group_row_view,null));
        return viewHolder;
    }

    @Override
    public void onBindSuspensionViewHolder(SuspensionViewHolder viewHolder, int position) {
        if(rowDataLocate.get(position) instanceof Integer){
            viewHolder.suspension.setText(datas.get(0).getGroupParamsViewModelList().get((Integer) rowDataLocate.get(position)).getGroupName());
        }else{
            HashMap<Integer,Integer> hashMap= (HashMap<Integer, Integer>) rowDataLocate.get(position);
            for (Integer key:hashMap.keySet()){
                viewHolder.suspension.setText(datas.get(0).getGroupParamsViewModelList().get(key).getGroupName());
            }
        }
    }

    @Override
    public boolean isPushGroupView(int tableFirstVisibleItemRowPosition) {
        return isBothGroup(tableFirstVisibleItemRowPosition,tableFirstVisibleItemRowPosition+1);
    }

    private boolean isBothGroup(int firstRowPosition,int secondRowPosition){
        Object firstValue=rowDataLocate.get(firstRowPosition);
        Object secondValue=rowDataLocate.get(secondRowPosition);
        int firstCompare=0;
        int secondCompare=0;
        if(firstValue instanceof Integer){
            firstCompare= (int) firstValue;
        }else{
            HashMap<Integer,Integer> firstMap= (HashMap<Integer, Integer>) firstValue;
            for (Integer key:firstMap.keySet()){
                firstCompare=key;
            }
        }
        if(secondValue instanceof Integer){
            secondCompare= (int) secondValue;
        }else{
            HashMap<Integer,Integer> firstMap= (HashMap<Integer, Integer>) secondValue;
            for (Integer key:firstMap.keySet()){
                secondCompare=key;
            }
        }
        return !(firstCompare==secondCompare);
    }

    class CornerViewHolder extends TableAdapter.ViewHolder{
        private TextView cornerCell;

        public CornerViewHolder(View itemView) {
            super(itemView);
            cornerCell=itemView.findViewById(R.id.corner_cell);
        }
    }

    class TopCellViewHolder extends RecyclerView.ViewHolder{
        private TextView top_cell;

        public TopCellViewHolder(View itemView) {
            super(itemView);
            top_cell=itemView.findViewById(R.id.top_cell);
        }
    }
    class GroupRowViewHolder extends RecyclerView.ViewHolder{
        private TextView group_row;

        public GroupRowViewHolder(View itemView) {
            super(itemView);
            group_row=itemView.findViewById(R.id.group_row);
        }
    }
    class LeftCellViewHolder extends TableAdapter.ViewHolder{
        private TextView left_cell;

        public LeftCellViewHolder(View itemView) {
            super(itemView);
            left_cell=itemView.findViewById(R.id.left_cell);
        }
    }

    class CellViewHolder extends RecyclerView.ViewHolder{
        private TextView cell;

        public CellViewHolder(View itemView) {
            super(itemView);
            cell=itemView.findViewById(R.id.cell);
        }
    }

    class SuspensionViewHolder extends TableAdapter.ViewHolder{
        private TextView suspension;
        public SuspensionViewHolder(View itemView) {
            super(itemView);
            suspension=itemView.findViewById(R.id.group_row);
        }
    }

    private void showToast(String message){
        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
    }
}
